package com.jiai.cloud.sys.controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.page.CustomPage;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.entity.GoodSearch;
import com.jiai.cloud.entity.TGoodsSpac;
import com.jiai.cloud.sys.service.ITGoodspacService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @佳爱科技
 * @since 2017-05-24
 */
@RestController
@RequestMapping("/api/v1/goodsSpac")
public class TGoodsSpacController {

    @Autowired
    private ITGoodspacService itGoodspacService;

    //增
    @RequestMapping(value = "/addGoodsSpac",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    public Object addGoodsSpac(@RequestBody TGoodsSpac tGoodsSpac)throws ServiceException {
        try{
            itGoodspacService.insert(tGoodsSpac);
            return RestResultGenerator.genSuccessResult("插入成功");
        }catch (Exception e){
            throw new ServiceException("插入失敗", e.getMessage());
        }
    }

    //通过ID删除
    @RequestMapping(value = "/del/{id}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object deleteId(@PathVariable("id") Integer id) throws ServiceException{
        try{
            itGoodspacService.deleteById(id);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }


    //查所有
    @RequestMapping(value = "/findByAll",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    public Object findByAll()  throws ServiceException{
        try {
            Page<TGoodsSpac> page= new Page<>(1, 20);
            itGoodspacService.selectPage(page, null);
            return RestResultGenerator.genSuccessResult(new CustomPage(page));
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    //根据ID查找
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findById(@PathVariable("id") Integer id) throws ServiceException {
        try {
            itGoodspacService.selectById(id);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }

    //改(有就改，没有就插入)
    @RequestMapping(value="/edit",method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object edit(@RequestBody TGoodsSpac tGoodsSpac)throws ServiceException{
        try {
            itGoodspacService.insertOrUpdate(tGoodsSpac);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new  ServiceException("编辑失敗",e.getMessage());
        }
    }




}
